package com.yuexunit.operationman.nbsy.module.AfterOperation.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.AfterOperation.model.impl.AfterOperationModel;
import com.yuexunit.operationman.nbsy.module.AfterOperation.presenter.IAfterOperationPresenter;
import com.yuexunit.operationman.nbsy.module.AfterOperation.presenter.impl.AfterOperationPresenter;
import com.yuexunit.operationman.nbsy.module.AfterOperation.ui.view.IAfterOperationView;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/10 0010.
 */

public class Act_AfterOperation_Sign extends BaseKJActivity implements ISafeOperationSignView,IAfterOperationView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;

    @Bind(R.id.img_ao_sign_01)
    ImageView imgAoSign01;
    @Bind(R.id.btn_ao_sign_01)
    TextView btnAoSign01;
    @Bind(R.id.tv_ao_sign_01_time)
    TextView tvAoSign01Time;
    @Bind(R.id.tv_ao_sign_01_name)
    TextView tvAoSign01Name;

    @Bind(R.id.img_ao_sign_02)
    ImageView imgAoSign02;
    @Bind(R.id.btn_ao_sign_02)
    TextView btnAoSign02;
    @Bind(R.id.tv_ao_sign_02_time)
    TextView tvAoSign02Time;
    @Bind(R.id.tv_ao_sign_02_name)
    TextView tvAoSign02Name;

    @Bind(R.id.img_ao_sign_03)
    ImageView imgAoSign03;
    @Bind(R.id.btn_ao_sign_03)
    TextView btnAoSign03;
    @Bind(R.id.tv_ao_sign_03_time)
    TextView tvAoSign03Time;
    @Bind(R.id.tv_ao_sign_03_name)
    TextView tvAoSign03Name;

    @Bind(R.id.img_ao_sign_04)
    ImageView imgAoSign04;
    @Bind(R.id.btn_ao_sign_04)
    TextView btnAoSign04;
    @Bind(R.id.tv_ao_sign_04_time)
    TextView tvAoSign04Time;
    @Bind(R.id.tv_ao_sign_04_name)
    TextView tvAoSign04Name;

    @Bind(R.id.img_ao_sign_05)
    ImageView imgAoSign05;
    @Bind(R.id.btn_ao_sign_05)
    TextView btnAoSign05;
    @Bind(R.id.tv_ao_sign_05_time)
    TextView tvAoSign05Time;
    @Bind(R.id.tv_ao_sign_05_name)
    TextView tvAoSign05Name;

    LoginDialog loginDialog;
    LoadingDialog loadingDialog;
    private int type = -1;

    IAfterOperationPresenter presenter;
    ISafeOperationSignPresenter presenter_sign;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_afteroperation_sign);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(aty);
        presenter = new AfterOperationPresenter(this,new AfterOperationModel());
        presenter_sign = new SafeOperationSignPresenter(this,new SafeOperationSignModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientName());
        if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent()
    {
        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).getObDoctorSignUrl()!=null
                &&AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).getObDoctorSignUrl().length()>0){
            imgAoSign01.setVisibility(View.VISIBLE);
            btnAoSign01.setVisibility(View.GONE);
            tvAoSign01Time.setVisibility(View.VISIBLE);
            tvAoSign01Name.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).getObDoctorSignUrl())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgAoSign01);
            tvAoSign01Time.setText("签名时间:"+AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).getObDoctorSignTimeStr());
            tvAoSign01Name.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).getObDoctorSign());
        }

        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).getObDoctorSignUrl()!=null
                &&AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).getObDoctorSignUrl().length()>0){
            imgAoSign02.setVisibility(View.VISIBLE);
            btnAoSign02.setVisibility(View.GONE);
            tvAoSign02Time.setVisibility(View.VISIBLE);
            tvAoSign02Name.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).getObDoctorSignUrl())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgAoSign02);
            tvAoSign02Time.setText("签名时间:"+AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).getObDoctorSignTimeStr());
            tvAoSign02Name.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).getObDoctorSign());
        }

        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).getObDoctorSignUrl()!=null
                &&AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).getObDoctorSignUrl().length()>0){
            imgAoSign03.setVisibility(View.VISIBLE);
            btnAoSign03.setVisibility(View.GONE);
            tvAoSign03Time.setVisibility(View.VISIBLE);
            tvAoSign03Name.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).getObDoctorSignUrl())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgAoSign03);
            tvAoSign03Time.setText("签名时间:"+AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).getObDoctorSignTimeStr());
            tvAoSign03Name.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).getObDoctorSign());
        }

        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).getObDoctorSignUrl()!=null
                &&AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).getObDoctorSignUrl().length()>0){
            imgAoSign04.setVisibility(View.VISIBLE);
            btnAoSign04.setVisibility(View.GONE);
            tvAoSign04Time.setVisibility(View.VISIBLE);
            tvAoSign04Name.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).getObDoctorSignUrl())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgAoSign04);
            tvAoSign04Time.setText("签名时间:"+AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).getObDoctorSignTimeStr());
            tvAoSign04Name.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).getObDoctorSign());
        }

        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getDoctorSignUrl()!=null
                &&AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getDoctorSignUrl().length()>0){
            imgAoSign05.setVisibility(View.VISIBLE);
            btnAoSign05.setVisibility(View.GONE);
            tvAoSign05Time.setVisibility(View.VISIBLE);
            tvAoSign05Name.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getDoctorSignUrl())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgAoSign05);
            tvAoSign05Time.setText("签名时间:"+AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getDoctorSignTimeStr());
            tvAoSign05Name.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getDoctorSign());
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.btn_ao_sign_01, R.id.btn_ao_sign_02, R.id.btn_ao_sign_03, R.id.btn_ao_sign_04, R.id.btn_ao_sign_05})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.btn_ao_sign_01:
                showTypeDialog();
                type = 1;
                break;
            case R.id.btn_ao_sign_02:
                showTypeDialog();
                type = 2;
                break;
            case R.id.btn_ao_sign_03:
                showTypeDialog();
                type = 3;
                break;
            case R.id.btn_ao_sign_04:
                showTypeDialog();
                type = 4;
                break;
            case R.id.btn_ao_sign_05:
                showTypeDialog();
                type = 5;
                break;
        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if(signBean.getCode() == 200)
        {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            switch (type) {
                case 1:
                    imgAoSign01.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgAoSign01);
                    btnAoSign01.setVisibility(View.GONE);
                    tvAoSign01Time.setVisibility(View.VISIBLE);
                    tvAoSign01Time.setText("签名时间:"+str);
                    tvAoSign01Name.setVisibility(View.VISIBLE);
                    tvAoSign01Name.setText(signBean.getObject().getDoctorName());

                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).setObDoctorSign(signBean.getObject().getDoctorName());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).setObDoctorSignUrl(signBean.getObject().getSignPicture());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(0).setObDoctorSignTimeStr(str);
                    break;
                case 2:
                    imgAoSign02.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgAoSign02);
                    btnAoSign02.setVisibility(View.GONE);
                    tvAoSign02Time.setVisibility(View.VISIBLE);
                    tvAoSign02Time.setText("签名时间:"+str);
                    tvAoSign02Name.setVisibility(View.VISIBLE);
                    tvAoSign02Name.setText(signBean.getObject().getDoctorName());

                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).setObDoctorSign(signBean.getObject().getDoctorName());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).setObDoctorSignUrl(signBean.getObject().getSignPicture());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitObList().get(1).setObDoctorSignTimeStr(str);
                    break;
                case 3:
                    imgAoSign03.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgAoSign03);
                    btnAoSign03.setVisibility(View.GONE);
                    tvAoSign03Time.setVisibility(View.VISIBLE);
                    tvAoSign03Time.setText("签名时间:"+str);
                    tvAoSign03Name.setVisibility(View.VISIBLE);
                    tvAoSign03Name.setText(signBean.getObject().getDoctorName());

                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).setObDoctorSign(signBean.getObject().getDoctorName());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).setObDoctorSignUrl(signBean.getObject().getSignPicture());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(0).setObDoctorSignTimeStr(str);
                    break;
                case 4:
                    imgAoSign04.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgAoSign04);
                    btnAoSign04.setVisibility(View.GONE);
                    tvAoSign04Time.setVisibility(View.VISIBLE);
                    tvAoSign04Time.setText("签名时间:"+str);
                    tvAoSign04Name.setVisibility(View.VISIBLE);
                    tvAoSign04Name.setText(signBean.getObject().getDoctorName());

                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).setObDoctorSign(signBean.getObject().getDoctorName());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).setObDoctorSignUrl(signBean.getObject().getSignPicture());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisitPainList().get(1).setObDoctorSignTimeStr(str);
                    break;
                case 5:
                    imgAoSign05.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgAoSign05);
                    btnAoSign05.setVisibility(View.GONE);
                    tvAoSign05Time.setVisibility(View.VISIBLE);
                    tvAoSign05Time.setText("签名时间:"+str);
                    tvAoSign05Name.setVisibility(View.VISIBLE);
                    tvAoSign05Name.setText(signBean.getObject().getDoctorName());

                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().setDoctorSign(signBean.getObject().getDoctorName());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().setDoctorSignUrl(signBean.getObject().getSignPicture());
                    AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().setDoctorSignTimeStr(str);
                    break;
            }


            if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getAoVisitId() == null) {
                Map<String, String> params = new HashMap<>();
                params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.AFTEROPERATIONBEAN));
                presenter.insertAfterOperationDate(params);
            } else {
                Map<String, String> params = new HashMap<>();
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.AFTEROPERATIONBEAN));
                presenter.updateAfterOperationDate(params);
            }
        }
        else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void insertAfterOperationDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateAfterOperationDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
